package com.ckk.eurekaclient.service.mq;

import com.ckk.eurekaclient.util.JsonUtil;
import com.ckk.eurekaclient.vo.mq.SyncDataResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * MqSendService
 *
 * @author momo
 */
@Slf4j
@Service
public class MqSendService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * routing key 约定为 ‘#’
     */
    private static final String ROUTING_KEY = "#";

    /**
     * @param exchange    交换机
     * @param id          数据ID
     * @param type        业务类型
     * @param warehouseId 加油站编号
     * @param data        数据
     */
    public <T> void send(String exchange, String type, T data) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        SyncDataResVO dataVO = new SyncDataResVO(type, JsonUtil.obj2Str(data));
        byte[] bytes = JsonUtil.json2byte(dataVO);
        Message msg = new Message(bytes, messageProperties);
        log.info("消息发送");
        rabbitTemplate.convertAndSend(exchange, ROUTING_KEY, msg);
    }

    /**
     * 发送消息体到 rabbitmq
     *
     * @param exchange 交换机
     * @param t        消息对象
     */
//    public <T> void send(String exchange, String routingKey, T t) {
//        MessageProperties messageProperties = new MessageProperties();
//        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
//        byte[] bytes = JsonUtil.json2byte(t);
//        Message msg = new Message(bytes, messageProperties);
//        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
//    }
}
